package com.company.project.web;

import cn.apiclub.captcha.Captcha;
import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer;
import cn.apiclub.captcha.gimpy.FishEyeGimpyRenderer;
import cn.apiclub.captcha.text.producer.TextProducer;
import cn.apiclub.captcha.text.renderer.DefaultWordRenderer;
import com.company.project.redis.CaptchaRedis;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author chenyang
 * @desc  获取验证码
 * @date 2017-07-24
 */
@RestController
@RequestMapping("captcha")
public class CaptchaModuleController {
    @Autowired
    private CaptchaRedis captchaRedis;

    private static int captchaW = 150;
    private static int captchaH = 60;

    @RequestMapping(value = "getcaptcha", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody   byte[] getCaptcha()
    {
        String uuid = UUID.randomUUID().toString();
        Captcha captcha = new Captcha.Builder(captchaW, captchaH)
                .addText().addBackground(new GradiatedBackgroundProducer())
                .gimp(new FishEyeGimpyRenderer())
                .build();
        //将验证码以<key,value>形式缓存到redis
        captchaRedis.setModelValue(uuid, captcha.getAnswer());
//        redisTemplate.opsForValue().set(uuid, captcha.getAnswer(), captchaExpires, TimeUnit.SECONDS);
        // ----------------自定义字体大小-----------
        // 自定义设置字体颜色和大小 最简单的效果 多种字体随机显示
          List fontList = new ArrayList<Font>();
        fontList.add(new Font("宋体", Font.HANGING_BASELINE, 40));// 可以设置斜体之类的
        fontList.add(new Font("Courier", Font.ITALIC, 40));
        fontList.add(new Font("宋体", Font.PLAIN, 40));
//        // 加入多种颜色后会随机显示 字体空心
        List<Color> colorList = new ArrayList<Color>();
        colorList.add(Color.orange);
        colorList.add(Color.BLACK);
        DefaultWordRenderer cwr = new DefaultWordRenderer(colorList, fontList);
        // 设置背景渐进效果 以及颜色 form为开始颜色，to为结束颜色
        GradiatedBackgroundProducer gbp = new GradiatedBackgroundProducer();
        gbp.setFromColor(Color.BLUE);
        gbp.setToColor(Color.WHITE);
        //生成验证码
           final String captchaValue = captchaRedis.getModelValue(uuid);
        if (StringUtils.isNotEmpty(captchaValue))
        {
            captcha = new Captcha.Builder(captchaW, captchaH).addText(new TextProducer() {
                        @Override
                        public String getText() {
                            return captchaValue;
                        }
                    },cwr)
//                    .addBackground(new GradiatedBackgroundProducer())
//                    .gimp(new FishEyeGimpyRenderer(Color.gray,Color.gray))
                    .build();
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            try {
                ImageIO.write(captcha.getImage(), "png", bao);
                return bao.toByteArray();
            } catch (IOException e) {
                return null;
            }
        }
        return  null;
    }
}
